package com.tbvtc.agriculturalcreditchainserver2025.comment.iot;



import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tbvtc.agriculturalcreditchainserver2025.comment.iot.Enm.LED;
import com.tbvtc.agriculturalcreditchainserver2025.comment.iot.Enm.Sensor;
import com.tbvtc.agriculturalcreditchainserver2025.exception.FoodException;
import com.tbvtc.agriculturalcreditchainserver2025.utils.SpringContextUtil;
import org.springframework.data.redis.core.RedisTemplate;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

public class IoTAPI {

    final String USER_NAME = "17333489872";
    final String USER_PASSWORD = "17333489872syh";
//    final String DEVICE_ID = "1244486";
    final String DEVICE_ID = "1304415";
    // String accecsToken = "309C1B2709303BC3A578C2EA8476054D9AA46520AB60A81C1C93B8C879E5FF6B2B6BD8E68F5A9249D20C5AB3B1C2D2F0C0F4F415FF5BE12C20C286946EC7D64F5E70A18B17647829190866D0BEB0DA0BFC36165B42CAECE486049FC6AA18DB0C22B2C05D444630D6C35C0AC27790B53AFEFB924790C597E682AC048B854B47F324B15D6DAA42B145402D652FAA7B2C604ADBA244F11A39B9A1E0DB50A3E2378440EF2DF4977368D55B972D19ACCF171C476FC5F1FC383BD11A76376C6DB1422EED1DD9E395D525E077CA617644E3E4920AF489CF4F522B5F49DDE09C4BD8";
    final String CMD_URL = "http://api.nlecloud.com/Cmds";
    final String DEVICE_URL = "http://api.nlecloud.com/devices/";
    KguHttpClient http = new KguHttpClient();

//    @Autowired
//    @Qualifier("TredisTemplate")
    private final RedisTemplate redisTemplate = (RedisTemplate) SpringContextUtil.getBean("TredisTemplate");


    public String getAccecsToken() {
        String ioTToken = (String) redisTemplate.opsForValue().get("IoTToken");
        if (ioTToken != null){
            return ioTToken;
        }
        HashMap<String,String> header = new HashMap();
        HashMap<String,String> body = new HashMap<>();
        header.put("Content-Type", "application/json");
        body.put("Account",USER_NAME);
        body.put("Password",USER_PASSWORD);
        String returnMsg = "";
        try {
            returnMsg = http.post("http://api.nlecloud.com/users/login", body , header);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        ObjectMapper mapper = new ObjectMapper();
        Response response = null;
        try {
            response = mapper.readValue(returnMsg, Response.class);
        } catch (JsonProcessingException e) {
            throw new FoodException(500,"数据反序列错误");
        }
        String accessToken = response.getResultObj().getAccessToken();
        try {
            redisTemplate.opsForValue().set("IoTToken",accessToken,1, TimeUnit.DAYS);
        } catch (Exception e) {
            throw new FoodException(500,"存入redis失败");
        }
        return accessToken;
    }

    public String getLedData(LED led) {
        StringBuffer buffer = new StringBuffer();
        buffer.append(DEVICE_URL);
        buffer.append(DEVICE_ID);
        buffer.append("/Sensors/");
        buffer.append(led.name());

        HashMap<String,String> header = new HashMap<>();
        header.put("AccessToken",getAccecsToken());
        header.put("apiTag",led.name());
        String returnMsg = "";

        try {
            returnMsg = http.get(buffer.toString(),header);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return returnMsg;
    }

    public String getSensorsData(Sensor sensor) {
        StringBuffer buffer = new StringBuffer();
        buffer.append(DEVICE_URL);
        buffer.append(DEVICE_ID);
        buffer.append("/Sensors/");
        buffer.append(sensor.name());

        HashMap<String,String> header = new HashMap<>();
        header.put("AccessToken",getAccecsToken());
        header.put("apiTag",sensor.name());
        String returnMsg = "";

        try {
            returnMsg = http.get(buffer.toString(),header);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return returnMsg;
    }

    public String setLedData(LED led, boolean on_or_false) {
        HashMap<String,String> header = new HashMap();
        header.put("Content-Type", "application/json");
        header.put("AccessToken",getAccecsToken());

        StringBuffer buffer = new StringBuffer();
        buffer.append(CMD_URL);
        buffer.append("?deviceId=").append(DEVICE_ID);
        buffer.append("&apiTag=").append(led.toString());

        String data = String.valueOf(on_or_false ? 1 : 0);
        String returnMsg = "";
        try {
            returnMsg = http.post(buffer.toString(),data,header);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return returnMsg;
    }

    public String setFanData(boolean on_or_false) {
        HashMap<String,String> header = new HashMap();
        header.put("Content-Type", "application/json");
        header.put("AccessToken",getAccecsToken());
        StringBuffer buffer = new StringBuffer();
        buffer.append(CMD_URL);
        buffer.append("?deviceId=").append(DEVICE_ID);
        buffer.append("&apiTag=").append("fengshan");

        String data = String.valueOf(on_or_false ? 1 : 0);
        String returnMsg = "";
        try {
            returnMsg = http.post(buffer.toString(),data,header);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return returnMsg;
    }
//---------------------------------------------------------------------------------------------------------//



//---------------------------------------------------------------------------------------------------------//
}
//---------------------------------------------------------------------------------------------------------//
// 在 ResultObj 类上添加注解忽略未知字段
//直接忽略出去accessToken以外的字段
@JsonIgnoreProperties(ignoreUnknown = true)
class ResultObj {
    @JsonProperty("AccessToken")
    private String accessToken;

    // 其他字段按需声明即可
    public String getAccessToken() {
        return accessToken;
    }
}

// 如果 Response 类也需要忽略其他字段
@JsonIgnoreProperties(ignoreUnknown = true)
class Response {
    @JsonProperty("ResultObj")
    private ResultObj resultObj;

    public ResultObj getResultObj() {
        return resultObj;
    }
}
